<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Picture;
use Illuminate\Http\Request;

class CommonController extends Controller {

	public function upload(Request $request)
	{
        $dir = trim($request->get('dir'));
        $subDir = date('Y-m-d');

		if ($request->hasFile('file'))
            $file = $request->file('file');
        else {
            return \Response::json([
                'id' => 0,
                'state' => 'fail',
                'message' => '未获取到上传文件，请刷新页面重试',
            ]);
        }

        $ext = $file->getClientOriginalExtension();
        $rand = rand(10000, 99999);
        $salt = strtoupper(md5(time() . $rand));
        $fileName = $salt . '.' . $ext;
        $fullPath = public_path().DS.'upload'.DS. ($dir ? $dir . DS : '') . $subDir .DS;

        $file->move($fullPath, $fileName);

        $pic = Picture::create([
            'user_id' => \Auth::user()->id,
            'dir' => $dir,
            'sub_dir' => $subDir,
            'pic' => $fileName,
        ]);

        return \Response::json([
            'id' => $pic->id,
            'pic' => $pic->pic,
            'dir' => $pic->dir,
            'state' => 'success',
            'message' => '上传成功',
        ]);
	}

    public function img($dir, $id, $width=100, $height=0, $up=1) {
        $nopic = public_path().DS.'images'.DS.$dir.'.jpg';
        $up = boolval($up);
        if (!file_exists($nopic)) {
            $nopic = public_path() . DS . 'images' . DS . 'nopic.gif';
        }

        $pic = Picture::find($id);
        if (! $pic)
            $file = $nopic;
        else {
            $file = public_path() . DS. 'upload'. DS . $pic->dir . DS . $pic->sub_dir . DS . $pic->pic;
            if (!file_exists($file)) {
                $file = $nopic;
            }
        }

        $width = intval($width) ? intval($width) : null;
        $height = intval($height) ? intval($height) : null;

        $image = \Intervention\Image\Facades\Image::make($file);
        if (is_null($width) || is_null($height)) {
            return $image->resize($width, $height, function ($constraint) use($up) {
                $constraint->aspectRatio();
                if ($up)
                    $constraint->upsize();
            })->response('png');
//            return $image->fit($width, $height, function ($constraint) use($up) {
//                $constraint->aspectRatio();
//                if ($up)
//                    $constraint->upsize();
//            })->response('png');
        } else {
            return $image->resize($width, $height)->response('png');
        }
    }

    public function options(Request $request)
    {

        $pid = intval($request->get('pid'));
        $model = ucfirst(trim($request->get('model')));
        $selected = intval($request->get('selected'));
        $defaultVal = trim($request->get('defaultVal'));
        $field = trim($request->get('field'));

        $model = 'App\\'.$model;
        $list = $model::where($field, '=', $pid)->get();
        $options = '<option value="'.$defaultVal.'">请选择</option>';
        foreach($list as $item) {
            $options .= '<option ' . ($item->id == $selected ? ' selected="selected"' : '') . ' value="'.$item->id.'">'.$item->name.'</option>';
        }

        return $options;
    }
}
